package com.zretc.controller;


import com.github.pagehelper.PageInfo;
import com.zretc.entity.FilmType;
import com.zretc.entity.Sales;
import com.zretc.service.SalesService;
import com.zretc.util.ExcelUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/back")
public class BackSaleexcleController {



    @Autowired
    private SalesService salesService;
    /**
     * 导出报表
     *
     * @return
     */
    @GetMapping("/export")
    @ResponseBody
    public void export(@RequestParam("page") String page,
                       @RequestParam("limit") String limit,
                       String filmName,
                       String screeningStartTime1,
                       String screeningStartTime2,
                       String auditoriumsName,
                       HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取数据
        Map<String,String> map =new HashMap<>();
        map.put("pageNum",page);
        map.put("pageSize",limit);
        map.put("filmName",filmName);
        map.put("screeningStartTime1",screeningStartTime1);
        map.put("screeningStartTime2",screeningStartTime2);
        map.put("auditoriumsName",auditoriumsName);
        PageInfo<Sales> companyByPage = salesService.getCompanyByPage(map);

        //excel标题
        String[] title = {"id","场厅","电影名称","时间","销售量","收入","上座率"};

        //excel文件名
        String fileName = "销售信息统计表" + System.currentTimeMillis() + ".xls";

        //sheet名
        String sheetName = "销售信息统计表";

        List<Sales> list = companyByPage.getList();
        String [][] content = new String[list.size()][7];

        for (int i = 0; i < list.size(); i++) {

            content[i] = new String[title.length];
            Sales sales = list.get(i);
            content[i][0] = i+1+"";
            content[i][1] = sales.getScreening().getAuditoriums().getAuditoriumsName();
            content[i][2] = sales.getScreening().getFilm().getFilmName();
            content[i][3] = sales.getScreening().getScreeningStartTime();
            content[i][4] = sales.getSalesCount().toString();
            content[i][5] = sales.getSalesPrice().toString();
            double a=sales.getSalesCount();
            DecimalFormat df=new DecimalFormat("0.00%");
            String attendance = df.format(a/100);
            content[i][6] = attendance;
        }

        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtils.getHSSFWorkbook(sheetName, title, content, null);

        //响应到客户端
        try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 发送响应流方法
     */
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(), "ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}


